package com.example.restaurantclient.Activity;



import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.restaurantclient.ConfigUtil.ConfigUtil;
import com.example.restaurantclient.Entities.Member;
import com.example.restaurantclient.R;
import com.google.gson.Gson;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class LoginActivity extends AppCompatActivity {
    private String reg = null;
    private String phone;
    private String name;
    private String pwd;
    public static Member member;

    private EditText etPhone;
    private EditText etPwd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        //设置背景透明度
        RelativeLayout layout=findViewById(R.id.login_bg);
        TextView textView=findViewById(R.id.tv_new_user);
        layout.getBackground().setAlpha(150);
        Button btnLogin=findViewById(R.id.login_commit);
        etPhone=findViewById(R.id.et_login_phone);
        etPwd=findViewById(R.id.et_login_password);
        //创建member对象
        member=new Member();
        Intent intent=getIntent();
        reg=intent.getStringExtra("Register");
        if(reg!=null){
            etPhone.setText(intent.getStringExtra("phone"));
            etPwd.setText(intent.getStringExtra("pwd"));
            member.setMemberName(intent.getStringExtra("name"));
            member.setMemberPhone(intent.getStringExtra("phone"));
            member.setMemberPwd(intent.getStringExtra("pwd"));
        }
        //点击登录按钮
        MyOnClickListener myOnClickListener=new MyOnClickListener();
        btnLogin.setOnClickListener(myOnClickListener);
        textView.setOnClickListener(myOnClickListener);

    }

    class MyOnClickListener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.tv_new_user:
                    //点击注册
                    final Intent intent=new Intent(view.getContext(),RegisterActivity.class);
                    startActivity(intent);
                    break;
                case R.id.login_commit:
                    //点击登录，访问服务器端
                    Intent intent1=new Intent(LoginActivity.this,FragmentActivity.class);
                    startActivity(intent1);
                    finish();
                    /*new Thread(){
                        @Override
                        public void run() {
                            super.run();
                            try {
                                URL url=new URL(ConfigUtil.SERVER_ADDR+"LoginMember");
                                HttpURLConnection connection=(HttpURLConnection) url.openConnection();
                                //设置请求方式
                                connection.setRequestMethod("POST");
                                OutputStream outputStream=connection.getOutputStream();
                                InputStream inputStream=connection.getInputStream();
                                member.setMemberPhone(etPhone.getText()+"");
                                member.setMemberPwd(etPwd.getText()+"");
                                Gson gson=new Gson();
                                outputStream.write(gson.toJson(member).getBytes());
                                byte[] buffer=new byte[256];
                                int len=inputStream.read(buffer);
                                String content=new String(buffer,0,len);
                                inputStream.close();
                                outputStream.close();
                                if (!content.equals("登录失败")){
                                    member=gson.fromJson(content,Member.class);
                                    Intent intent1=new Intent(LoginActivity.this,FragmentActivity.class);
                                    startActivity(intent1);
                                }else {
                                    Log.e("登录结果","登录失败");
                                }

                            } catch (MalformedURLException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }.start();*/
                    break;

            }
        }
    }
}